﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using DataTable = System.Data.DataTable;
using Word = Microsoft.Office.Interop.Word;

namespace ElectronicJournal.Account
{
    public partial class NewReview : System.Web.UI.Page
    {

        private Reviewer _reviewer;

        private Publication _currentPublication;
        DataTable ArticleTable = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {

            InitializeScientificDirections();
            InitializeConcludeRadioButtonList();

            _reviewer = 
                EJournalRepository.Instance.GetReviewerByUserId(
                    Guid.Parse(Membership.GetUser().ProviderUserKey.ToString()));



        }

        protected void InitializeScientificDirections()
        {
            List<ScientificDirection> scientificDirections = EJournalRepository.Instance.GetAllScientificDirections().ToList();
            foreach (var scientificDir in scientificDirections)
            {
                ScientificDirectionsList.Items.Add(new ListItem(scientificDir.Title, scientificDir.Title));
            }
            ScientificDirectionsList.SelectedIndex = 0;
        }

        protected void InitializeJournal()
        {

          //  Journal journal = EJournalRepository.Instance.
           // Journal.Text = journal.JournalName;
        }

        protected void InitializeConcludeRadioButtonList()
        {
            if (ConcludeRadioButtonList.Items.Count.Equals(0))
            {
                ConcludeRadioButtonList.Items.Add(
                    new ListItem("Рекомендовать  рукопись к опубликованию в сборнике статей", "1"));
                ConcludeRadioButtonList.Items.Add(
                    new ListItem("Рекомендовать  рукопись к опубликованию в сборнике статей с доработкой", "2"));
                ConcludeRadioButtonList.Items.Add(new ListItem("Отклонить", "3"));
                ConcludeRadioButtonList.SelectedIndex = 0;
            }


        }

        public void FormReview()
        {

           // OrganizationInformation information = EJournalRepository.Instance.GetOrganizationInformation();
            
          
            // пока возьмем только первую публикацию, потом необходимо сделать таблицу
           // var pub = publicationsForReviewer.First();


            object fileName = Server.MapPath(@".") + @"\files\reviewers\3.dot";
            Word.Application WordApp = new Application();
            WordApp.Visible = false;
            Word.Document WordDocument = WordApp.Documents.Open(fileName);

           //Научное направление
            object oBookMark = "Direction";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = ScientificDirectionsList.SelectedValue;

            //Заключение
            oBookMark = "Close";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = ConcludeTBx.Text + "\n" +
                ConcludeRadioButtonList.Items[ConcludeRadioButtonList.SelectedIndex].Text;
            
            //Наименование сборника
            

            //
            oBookMark = "T1";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T1.Text;
            oBookMark = "T2";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T2.Text;
            oBookMark = "T3";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T3.Text;
            oBookMark = "T4";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T4.Text;
            oBookMark = "T5";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T5.Text;
            oBookMark = "T6";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T6.Text;
            oBookMark = "T7";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T7.Text;
            oBookMark = "T8";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T8.Text;
            oBookMark = "T9";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T9.Text;
            oBookMark = "T10";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = T10.Text;

            oBookMark = "UDK";
            if (UDKCheckBx.Checked)
                WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = "присутствует";
            else
            {
                WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = "отсутствует";
            }



            oBookMark = "ReviewerFIO";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = _reviewer.ReviewerLastName + " " + _reviewer.ReviewerFirstName[0] + ". " + _reviewer.ReviewerThirdName[0] + ". ";

            oBookMark = "DateNow";
            WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = DateTime.Now.Date.ToShortDateString();
            
            //oBookMark = "FIO";
            //WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = author.AuthorFirstName + " " + author.AuthorSecondName + " " +
            //                                                            author.AuthorThirdName;
            //oBookMark = "FIO1";
            //WordDocument.Bookmarks.get_Item(ref oBookMark).Range.Text = author.AuthorFirstName + " " + author.AuthorSecondName + " " +
            //                                                            author.AuthorThirdName;

            WordDocument.SaveAs(Server.MapPath(@".") + @"\files\reviewers\8.doc");
            WordDocument.Application.Quit();

            // Сохранить путь к файлу рецензии в таблице рецензий

        }

        protected void SubmitAddBtn_Click(object sender, EventArgs e)
        {
            FormReview();
            MainPanel.Visible = false;
        }

        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            MainPanel.Visible = false;
        }

    }
}